clc
clear all
close all

%读取数据
filename='JanuaryDatasetTrain.xlsx';
WaterTable = readtable(filename);
WaterTable.WaterChangeStatus = sign(WaterTable.NowWater - WaterTable.PreviousWater);
%时间和数据分开保存
timestamps=WaterTable.Time;
data=WaterTable(:,2:end);
data=table2array(data); %输入的数据

%初始化
WaterChange = ones(length(data),1);  %初始化输出值
Status = zeros(length(data),1); %初始化排查状态量
SubStatus = zeros(length(data),1);%初始化排查状态量

CurrentA_low = 130;
CurrentA_high = 150;
CurrentB_low = 130;
CurrentB_high = 150;

for i = 1:length(data)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %异常状态检测
    %Totalstatus[20]
    Totalstatus = data(i,19);
    %SchemeStatus[25]
    SchemeStatus = data(i,24);
    %OnlyReturnStatus[26]
    OnlyReturnStatus = data(i,25);

    %A电流区间[37 38]
    CurrentShreholdA = [100 data(i,36) data(i,37) 200];
    %B电流区间[41 42]
    CurrentShreholdB = [100 data(i,40) data(i,41) 200];
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %根据电流和成球率分成四个模块
    %棍子设定转速[15、16]
    SpeedSetA = data(i,14);
    SpeedSetB = data(i,15);

    %压球机AB电流[7,11]
    CurrentA = data(i,6);
    CurrentB = data(i,10);

    %压球机AB电流_1min/2min/3min[8-10,12-14]
    CurrentA_1min = data(i,7);
    CurrentA_2min = data(i,8);
    CurrentA_3min = data(i,9);
    CurrentB_1min = data(i,11);
    CurrentB_2min = data(i,12);
    CurrentB_3min = data(i,13);

    %电流一天平均数[35\39]
    CurrentA_Average = data(i,34);
    CurrentB_Average = data(i,38);
    %BallRatio[27]
    BallRatio = data(i,26);

    %ZZ208[28]
    ZZ208 = data(i,27);

    %ZZ203AB[29、30]
    ZZ203A = data(i,28);
    ZZ203B = data(i,29);

    %TotalWeight[19]
    %Totalpercent[24]
    TotalWeight = data(i,18)*data(i,23)./100;

    %CurrentA_ExtraElement[35-38]
    %CurrentB_ExtraElement[39-42]
    CurrentA_ExtraElement = data(i,[34,36,37]);
    CurrentB_ExtraElement = data(i,[38,40,41]);
    %返料仓十分钟速度[6]
    ReturnBin_10min = data(i,5);

    %返料仓仓位[5]
    ReturnBin = data(i,4);

    %1过去一天平均值改变上下区间，2机器学习
    DecisionMode = 1;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    [WaterChange(i),Status(i),SubStatus(i)] = WaterController(OnlyReturnStatus,...
    SchemeStatus,Totalstatus,BallRatio,ZZ208,ZZ203A,ZZ203B,ReturnBin,ReturnBin_10min,...
    CurrentA,CurrentA_1min,CurrentA_2min,CurrentA_3min,...
    CurrentB,CurrentB_1min,CurrentB_2min,CurrentB_3min,...
    SpeedSetA,SpeedSetB,TotalWeight,...
    CurrentA_Average,CurrentB_Average,...
    DecisionMode,CurrentA_low,CurrentA_high,CurrentB_low,CurrentB_high);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
Output = table(WaterChange,Status,SubStatus);
Output = [Output WaterTable];
writetable(Output,"Test.xlsx");